<?php

namespace app\common\model\Subject\Teacher;

use app\common\model\Subject\Subject;
use think\Model;
use traits\model\SoftDelete;


class Teacher extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'subject_teacher';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = 'deletetime';

    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [
        'follows',
        'avatar_text',
        'subject_num'
    ];
    public function getFollowsAttr($value,$data){
        if(!isset($data['id']))return;
        return $this->hasMany('follow','teacherid')->field('id,busid,createtime')->select()->toArray();
    }
    public function getSubjectNumAttr($value,$data){
        $num = $this->hasMany('app\common\model\Subject\Subject','teacherid')
                ->select()
                ->count();
        return $num;
    }
    public function getAvatarTextAttr($value,$data){
        $teacherDefaultImg = is_file(ROOT_PATH.'public'.config('site.teacherDefaultImg'))?config('site.teacherDefaultImg'):'/assets/home/images/teacherDefaultImg.gif';
        return is_file(ROOT_PATH.'public'.$data['avatar'])?$data['avatar']:$teacherDefaultImg;
    }
}
